﻿
Partial Class Control_ucAutoComplete
    Inherits controlMatrix 'System.Web.UI.UserControl
    Protected mTableName As String
    Public Property TableName() As String
        Get
            Return mTableName
        End Get
        Set(ByVal value As String)
            mTableName = value
        End Set
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (txtID.Text.Length.Equals(0)) Then
            txtDisplay.Text = String.Empty
            txtName.Text = String.Empty
        End If


    End Sub

    Public Function GetAutoCompleteScript() As String
        '0→ID 1→table name 2→顯示名稱用的ID
        Dim enter As String
        enter = Environment.NewLine

        GetAutoCompleteScript = "$(function() {" + enter + _
"          $('#" + txtID.ClientID + "').autocomplete('QMG2.ASPX'," + enter + _
"            {" + enter + _
"                delay: 30," + enter + _
"                width: 250," + enter + _
"                minChars: 1, //至少輸入幾個字元才開始給提示?" + enter + _
"                matchSubset: false," + enter + _
"                matchContains: false," + enter + _
"                cacheLength: 0," + enter + _
"                noCache: true, " + enter + _
"                onItemSelect: findValue," + enter + _
"                onFindValue: findValue," + enter + _
"                formatItem: function(row) {" + enter + _
"                    return ""<div style='height:12px'><div style='float:left'>"" + row[0] +" + enter + _
"                            ""</div>"" +" + enter + _
"                            ""<div style='float:right;padding-right:5px;'>"" +" + enter + _
"                            row[1] + ""</div>"";" + enter + _
"                }," + enter + _
"                extraParams: { t: '" + TableName + "' }, " + enter + _
"                autoFill: false," + enter + _
"                mustMatch: false //是否允許輸入提示清單上沒有的值?" + enter + _
"            });" + enter + _
"              function findValue(li) {" + enter + _
"                  if (li == null) return alert('No match!');" + enter + _
"                  $('#" + txtID.ClientID + "').val(li.selectValue);" + enter + _
"                  $('#" + txtName.ClientID + "').val(li.extra[0]);" + enter + _
"                  $('#" + txtDisplay.ClientID + "').val(li.extra[0]);" + enter + _
"              }" + enter + _
"          });"
        'setTimeout(function() { //給值}, 1); autoFill: false,由true 改 false
        '"                noCache: true, " + enter + _  '//黑暗版自訂參數，每次都重新連後端查詢(適用總資料筆數很多時)

    End Function

    Public Function GetValue() As String
        GetValue = txtID.Text
    End Function
    Public Function GetName() As String
        txtDisplay.Text = txtName.Text
        GetName = txtName.Text
    End Function

    Public Sub SetName(ByVal p_name As String)
        txtDisplay.Text = p_name
    End Sub
    '--確認是否已有選擇
    Public Function IsHaveValue() As Boolean
        IsHaveValue = (IIf(txtName.Text.Length > 0, True, False))
    End Function
End Class
